# RELEASE DEBUG
export buildType=$1
branch=$2
clean=$3
logFile="-"
basePath="/Users/weli/WorkSpace/mateurp"
projectPath=${basePath}/mateurp
frameworkPath=${projectPath}/Build/UnitySDK
unity=/Applications/Unity/Hub/Editor/2022.3.61f1c1/Unity.app/Contents/MacOS/Unity
cd $basePath

# Update Code
echo Update Code Start
git checkout $branch
git fetch origin || exit 1
git reset --hard "origin/$branch" || exit 1
# Update Code Finish

# GenerateAll
echo GenerateAll Start
$unity -batchmode -nographics -quit -projectPath $projectPath -buildTarget iOS -executeMethod HybridCLR.Editor.BuildAssetsCommand.Prebuild -logFile - || exit 1
echo GenerateAll Finish

# BuildAssets
echo BuildAssets Start
rm -rf $projectPath/Assets/StreamingAssets
rm -rf $projectPath/HybridCLRBuildCache
$unity -batchmode -nographics -quit -projectPath $projectPath -executeMethod HybridCLR.Editor.BuildAssetsCommand.BuildAndCopyABAOTHotUpdateDlls -logFile $logFile || exit 1
echo BuildAssets Finish

# BuildArts
echo BuildArts Start
$unity -batchmode -nographics -quit -projectPath $projectPath -executeMethod HybridCLR.Editor.BuildAssetsCommand.BuildBuiltInArtAssetsAB -logFile $logFile || exit 1
echo BuildArts Finish

# BuildXCODE
echo BuildXCODE Start
rm -rf $projectPath/Build/IOS/$buildType
$unity -batchmode -nographics -quit -projectPath $projectPath -executeMethod BuildTool.BuildIPAScript -logFile $logFile || exit 1
echo BuildXCODE Finish

# BuildFramework
echo BuildFramework
if [ -d $frameworkPath ];then
    rm -rf $frameworkPath
fi
mkdir $frameworkPath
mkdir $frameworkPath/Data
cp -r $projectPath/Build/IOS/$buildType/output_xcode/Data/* $frameworkPath/Data
xcodebuild clean -project $projectPath/Build/IOS/$buildType/output_xcode/Unity-iPhone.xcodeproj -target UnityFramework || exit 1
xcodebuild build -project $projectPath/Build/IOS/$buildType/output_xcode/Unity-iPhone.xcodeproj -target UnityFramework || exit 1
cp -r $projectPath/Build/IOS/$buildType/output_xcode/build/Release-iphoneos/UnityFramework.framework/* $frameworkPath || exit 1
echo BuildFramework Finish
